import { createRouter, createWebHashHistory } from 'vue-router'
import index from '../views/home/index.vue'
import Cookies from 'js-Cookie'
import { CheckReleaseDistrict } from '../http/api'
import { start, close } from '../utils/nporgress/index'

const routes = [
  {
    path: '/',
    component: index,
    redirect: '/index',
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('../views/home/index/index.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
  },
  {
    path: '/nav',
    name: 'nav',
    component: () => import('../views/nav/index.vue'),
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('../views/login/reg.vue'),
  },
  {
    path: '/questionnaire/:id',
    name: 'questionnaire',
    component: () => import('../views/questionnaire/index.vue'),
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/home/queryUser',
    component: () => import('../views/backstage/index.vue'),

    children: [
      {
        path: 'queryUser',
        name: 'queryUser',
        meta: { admin: ['super_admin', 'high_admin'] },
        component: () => import('../views/backstage/queryUser/index.vue'),
      },
      {
        path: 'profile',
        name: 'profile',
        meta: { admin: ['super_admin', 'high_admin'] },
        component: () => import('../views/backstage/profile/index.vue'),
      },
      {
        path: 'openQuestionnaires',
        name: 'openQuestionnaires',
        meta: { admin: ['super_admin'] },
        component: () => import('../views/backstage/openQuestionnaire/index.vue'),
      },
      {
        path: 'openQuestionnaireh',
        name: 'openQuestionnaireh',
        meta: { admin: ['super_admin', 'high_admin'] },
        component: () => import('../views/backstage/openQuestionnaire/openQuestionnaireh.vue'),
      },
      {
        path: 'openQuestionnairem',
        name: 'openQuestionnairem',
        meta: { admin: ['super_admin', 'mid_admin'] },
        component: () => import('../views/backstage/openQuestionnaire/openQuestionnairem.vue'),
      },
      {
        path: 'viewQuestionnaire',
        name: 'viewQuestionnaire',
        meta: { admin: ['super_admin', 'high_admin'] },
        component: () => import('../views/backstage/viewQuestionnaire/index.vue'),
      },
      {
        path: 'viewQuestionnaire_school',
        name: 'viewQuestionnaire_school',
        meta: { admin: ['high_admin', 'mid_admin'] },
        component: () => import('../views/backstage/viewQuestionnaire/school.vue'),
      },
      {
        path: 'user',
        name: 'user',
        meta: {},
        component: () => import('../views/backstage/user/user.vue'),
      },
      {
        path: 'user/pwd',
        name: 'user/pwd',
        meta: {},
        component: () => import('../views/backstage/user/pwd.vue'),
      },
    ],
  },
]

export const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})

router.beforeEach(async (to, _from, next) => {
  start()
  if (typeof to.meta.admin === 'object') {
    if (Cookies.get('LoginState') !== 'true') {
      next({ path: '/login' })
    }
    if (
      (to.meta.admin.includes('super_admin') && Cookies.get('super_admin') === 'true') ||
      (to.meta.admin.includes('high_admin') && Cookies.get('high_admin') === 'true') ||
      (to.meta.admin.includes('mid_admin') && Cookies.get('mid_admin') === 'true')
    ) {
      next()
      return
    } else
      next({
        name: 'user',
      })
  }
  if (to.path === '/login' || to.path === '/reg') {
    if (Cookies.get('LoginState') === 'true')
      next({
        path: '/nav',
      })
    else next()
  } else if (to.path === '/nav' || to.path === '/home/user') {
    if (Cookies.get('LoginState') === 'true') next()
    else
      next({
        path: '/login',
      })
  } else if (to.path === '/questionnaire/11') {
    if (Cookies.get('user_type') === '2' && Cookies.get('city_id') === '11') {
      if (await CheckReleaseDistrict()) {
        next()
      } else next({ path: '/nav' })
    } else next({ path: '/nav' })
  } else if (to.path === '/questionnaire/1') {
    if (Cookies.get('user_type') === '1') {
      if (await CheckReleaseDistrict()) {
        next()
      } else next({ path: '/nav' })
    } else next({ path: '/nav' })
  } else if (to.path === '/questionnaire/12') {
    if (Cookies.get('user_type') === '2' && Cookies.get('city_id') !== '11') {
      if (await CheckReleaseDistrict()) {
        next()
      } else next({ path: '/nav' })
    } else next({ path: '/nav' })
  } else next()
})

router.afterEach(() => {
  close()
})
export default router
